package org.gw.building.ctrl;

import org.gw.building.model.Tyxxcjsz;
import org.gw.building.repositories.TyxxcjszRepository;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.interceptor.RequestPermission;
import org.gw.ylc.base.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Iterator;

/**
 * Created by qwx89 on 2017/8/6.
 */
@RestController
@RequestMapping("tyxxcjsz")
public class TyxxcjszCtrl {

    @Autowired
    private TyxxcjszRepository tyxxcjszRepository;

    @RequestMapping("query")
    @RequestPermission({RequestPermission.SCHOOL})
    public Tyxxcjsz query() {
        Iterable<Tyxxcjsz> cjszs = tyxxcjszRepository.findAll();
        Iterator<Tyxxcjsz> it;
        Tyxxcjsz cjsz;
        if(cjszs != null && (it = cjszs.iterator()) != null && it.hasNext()){
            cjsz = it.next();
        }else{
            cjsz = new Tyxxcjsz();
            UserInfo userInfo = RequestContextHolder.get().getUser();
            cjsz.setXgr(userInfo.getName());
            cjsz.setXgsj(new Date());
        }
        return cjsz;
    }
    @RequestMapping("edit")
    @RequestPermission({RequestPermission.SCHOOL})
    public Object edit(Tyxxcjsz cjsz){
        UserInfo userInfo = RequestContextHolder.get().getUser();
        cjsz.setXgr(userInfo.getName());
        cjsz.setXgsj(new Date());
        return tyxxcjszRepository.save(cjsz);
    }
}
